/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-02-17
*/
package com.rzico.basics.controller.admin;

import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.ProductCategory;
import com.rzico.basics.entity.Supplier;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.ProductCategoryService;
import com.rzico.basics.service.SupplierService;
import com.rzico.core.entity.SysUser;
import com.rzico.core.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品分类控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-02-17
 */
@Api(description = "产品分类接口")
@RestController
@RequestMapping("/admin/productCategory")
public class ProductCategoryAdminController extends BaseController {

    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 分页查询产品分类
     *
     * @return
    */
    @ApiOperation("分页查询产品分类")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "分类ID", dataType = "Long", paramType = "query")})
    public CommResult<ProductCategory> list(Long id) {
        Map<String, Object> params = new HashMap<String, Object>();

        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        if (null != id){
            params.put("treePath", id);
        }
        params.put("sortField", "tree_path");
        params.put("sortType", "asc");
        List<ProductCategory> list = productCategoryService.selectTree(params);
        return CommResult.success(list);
    }

    /**
     * 查询单条产品分类
     *
     * @return
    */
    @ApiOperation("查询单条产品分类")
    @GetMapping("/find/{id}")
    public CommResult<ProductCategory> find(@PathVariable String id) {
        ProductCategory result = productCategoryService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("productCategory",result);
        return CommResult.success(result);

    }

    /**
     * 保存产品分类
     *
     * @param productCategory
     * @return
     */
    @ApiOperation("保存产品分类")
    @PostMapping("/save")
    public CommResult<ProductCategory> save(@RequestBody ProductCategory productCategory) {
        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise==null) {
            return CommResult.error("没有开通企业");
        }

        //检查是否供应商账号
        SysUser sysUser = sysUserService.getCurrent();
        Supplier supplier = supplierService.findByUserId(sysUser.getId());
        if (supplier!=null) {
            return CommResult.error("供应商账号无权修改");
        }

        productCategory.setIsShow(true);
        productCategory.setEnterpriseId(enterprise.getId());
        productCategory.setCreateDate(new Date());

        int affectCount = productCategoryService.insertUseGeneratedKeys(productCategory);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        ProductCategory result = productCategoryService.findById(productCategory.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除产品分类
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除产品分类", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除产品分类,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<ProductCategory> del(@PathVariable String ids) {

        productCategoryService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新产品分类
     *
     * @param productCategory
     * @return
     */
    @Log(desc = "更新产品分类", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新产品分类")
    @PostMapping("/update")
    public CommResult<ProductCategory> update(@RequestBody ProductCategory productCategory) {

        //检查是否供应商账号
        SysUser sysUser = sysUserService.getCurrent();
        Supplier supplier = supplierService.findByUserId(sysUser.getId());
        if (supplier!=null) {
            return CommResult.error("供应商账号无权修改");
        }

        int affectCount = productCategoryService.updateByPrimaryKeySelective(productCategory);
        if (affectCount <= 0){
            return CommResult.error();
        }
        ProductCategory result = productCategoryService.findById(productCategory.getId());
        return CommResult.success(result);
    }

}
